//
//  UserAccount.swift
//  我的微博练习
//
//  Created by zhangguo on 15/10/11.
//  Copyright © 2015年 zhangguo. All rights reserved.
//

import UIKit

class UserAccount: NSObject, NSCoding{
    
    //用户是否登录标记
    class var userLogon:Bool {
        return shareAccount != nil
    }
    
    var access_token:String?
    //生命周期是一个具体数值
    var expires_in:NSTimeInterval = 0 {
        didSet{
             exprinDate = NSDate(timeIntervalSinceNow: expires_in)
        }
    }
    //过期日期
    var exprinDate:NSDate?
    
    var uid:String?
    
    //友好显示名称
    var name:String?
    //头像地址大图
    var avatar_large:String?
    
    init(dict: [String:AnyObject]) {
        super.init()
        setValuesForKeysWithDictionary(dict)
        //设置全局的用户信息
       UserAccount.userAccount = self
    }
    
    override func setValue(value: AnyObject?, forUndefinedKey key: String) {}
    
    //对象描叙信息
   override var description: String {
        let properti = ["access_token","expires_in","uid","exprinDate","name","avatar_large"]
    
        return "\(dictionaryWithValuesForKeys(properti))"
    }
    //加载用户信息
    func loadUserInfo(finished:(error: NSError?)->()){
        NetWorkingTools.sharedTools.loadUserInfo(uid!) { (result, error) -> () in
           
            if error != nil{
                finished(error: error)
                return
            }
            // 设置用户信息
            self.name = result!["name"] as? String
            
            self.avatar_large = result!["avatar_large"] as? String
            
            //b保存用户信息
            self.saveAccount()
            //返回回调
           finished(error: nil)
        }
    }
    
//    保存归档解档路径
    static private let accountPath = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true).last!.stringByAppendingString("/account.plist")
    
    //保存账户信息
    func saveAccount(){
        NSKeyedArchiver.archiveRootObject(self, toFile: UserAccount.accountPath)
    }
    
    //加载账户信息
      private static var userAccount:UserAccount?
      class var shareAccount: UserAccount? {
        
//       //判断静态账户是否存在，不存在，需要解档
        //1判断用户是否存在
        if userAccount == nil {
            
            userAccount = NSKeyedUnarchiver.unarchiveObjectWithFile(accountPath) as? UserAccount
        }
        
        //2,判断日期
        if let date = userAccount?.exprinDate where date.compare(NSDate()) == NSComparisonResult.OrderedAscending {
            //如果已经过期，需要清空账号记录
            userAccount = nil
        }
        return userAccount
    }
    
    //归档，
    func encodeWithCoder(aCoder: NSCoder){
        aCoder.encodeObject(access_token, forKey: "access_token")
        aCoder.encodeDouble(expires_in, forKey: "expires_in")
        aCoder.encodeObject(exprinDate, forKey: "exprinDate")
        aCoder.encodeObject(uid, forKey: "uid")
        aCoder.encodeObject(name,forKey:"name")
        aCoder.encodeObject(avatar_large, forKey: "avatar_large")
        
    }
    //解档
    required init?(coder aDecoder: NSCoder) {
        access_token = aDecoder.decodeObjectForKey("access_token") as? String
        expires_in = aDecoder.decodeDoubleForKey("expires_in")
        exprinDate = aDecoder.decodeObjectForKey("exprinDate") as? NSDate
         uid = aDecoder.decodeObjectForKey("uid") as? String
        name = aDecoder.decodeObjectForKey("name") as? String
        avatar_large = aDecoder.decodeObjectForKey("avatar_large") as? String
        
    }
}
